package io.renren.modules.seller.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.modules.seller.dao.UserDao;
import io.renren.modules.seller.entity.PageResult;
import io.renren.modules.seller.entity.UserEntity;
import io.renren.modules.seller.service.UserService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service("userService")
public class UserServiceImpl extends ServiceImpl<UserDao, UserEntity> implements UserService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<UserEntity> page = this.page(
                new Query<UserEntity>().getPage(params),
                new QueryWrapper<UserEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageResult findPage(UserEntity userEntity, Map<String, Object> params) {
        String username = userEntity.getUsername();
        String nickName = userEntity.getNickName();
        String status = userEntity.getStatus();
        IPage<UserEntity> page = this.page(
                new Query<UserEntity>().getPage(params),
                new QueryWrapper<UserEntity>()
                        .eq(StringUtils.isNotBlank(username), "username", username)
                        .eq(StringUtils.isNotBlank(nickName), "nick_name", nickName)
                        .eq(StringUtils.isNotBlank(status), "status", status)
        );
        return new PageResult(page.getTotal(), page.getRecords());
    }

    @Override
    public PageResult findPage(Map<String, Object> params) {
        IPage<UserEntity> page = this.page(
                new Query<UserEntity>().getPage(params),
                new QueryWrapper<UserEntity>()
        );
        return new PageResult(page.getTotal(), page.getRecords());
    }

}
